﻿using OpenAdmin.Core.Consts;
using OpenAdmin.Core.Enums;
using OpenAdmin.Core.Models;
using OpenAdmin.ToolKits;

namespace OpenAdmin.EntityFramework.SeedDataConfigure;

/// <summary>
/// 权限相关数据
/// </summary>
public class SeedData
{
    private static Guid PlatformTenantId => new Guid("BE066E77-C7A8-9181-5E9D-5457ACD286BF");
    private static Guid EnterpriseTenantId => new Guid("D4BB6BCA-0DA1-BFED-7DF5-209F09641AEF");
    /// <summary>
    /// 获取租户数据
    /// </summary>
    /// <returns></returns>
    public static List<EFCoreTenant> GetTenant()
    {
        return new List<EFCoreTenant>
        {
            new EFCoreTenant
            {
                TenantId = PlatformTenantId,
                Name = "平台租户",
                Enabled = true,
                Remark = "平台租户",
                Email = "superAdmin@163.com",
                PhoneNumber = "13680398478"
            },
            new EFCoreTenant
            {
                TenantId = EnterpriseTenantId,
                Name = "企业租户01",
                Enabled = true,
                Remark = "企业租户",
                Email = "johnny@163.com",
                PhoneNumber = "13680398456"
            },
        };
    }

    /// <summary>
    /// 获取用户数据
    /// </summary>
    /// <returns></returns>
    public static List<SysUser> GetSysUsers()
    {
        return new List<SysUser>()
        {
            new SysUser { TenantId = PlatformTenantId, Id = 1, UserName = "superAdmin", Password = MD5Encryption.MD5Encrypt32("123456"), RealName = "超级管理员", Avatar = "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif", Status = 1, UserType = UserType.SuperAdmin, CreateBy = 1 },
            new SysUser { TenantId = PlatformTenantId, Id = 2, UserName = "admin", Password = MD5Encryption.MD5Encrypt32("123456"), RealName = "系统管理员", Avatar = "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif", Status = 1, UserType = UserType.SystemAdmin, CreateBy = 1 },
            new SysUser { TenantId = PlatformTenantId, Id = 3, UserName = "johnny", Password = MD5Encryption.MD5Encrypt32("123456"), RealName = "普通用户", Avatar = "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif", Status = 1, UserType = UserType.None, CreateBy = 1 },

            new SysUser { TenantId = EnterpriseTenantId, Id = 4, UserName = "johnny@163.com", Password = MD5Encryption.MD5Encrypt32("123456"), RealName = "系统管理员", Avatar = "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif", Status = 1, UserType = UserType.SystemAdmin, CreateBy = 1 },
            new SysUser { TenantId = EnterpriseTenantId, Id = 5, UserName = "user01@163.com", Password = MD5Encryption.MD5Encrypt32("123456"), RealName = "普通用户", Avatar = "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif", Status = 1, UserType = UserType.None, CreateBy = 1 },
        };
    }

    /// <summary>
    /// 获取角色数据
    /// </summary>
    /// <returns></returns>
    public static List<SysRole> GetSysRoles()
    {
        return new List<SysRole>()
        {
            new SysRole { TenantId = PlatformTenantId, Id = 1, Name = "系统管理员", Enabled = true, Remark = "系统管理员", CreateBy = 1 },
            new SysRole { TenantId = PlatformTenantId, Id = 2, Name = "普通用户", Enabled = true, Remark = "普通用户", CreateBy = 1 },

            new SysRole { TenantId = EnterpriseTenantId, Id = 3, Name = "系统管理员", Enabled = true, Remark = "系统管理员", CreateBy = 1 },
            new SysRole { TenantId = EnterpriseTenantId, Id = 4, Name = "普通用户", Enabled = true, Remark = "普通用户", CreateBy = 1 },
        };
    }

    /// <summary>
    /// 获取用户角色数据
    /// </summary>
    /// <returns></returns>
    public static List<SysUserRole> GetSysUserRoles()
    {
        return new List<SysUserRole>()
        {
            new SysUserRole { UserId = 2, RoleId = 1 },
            new SysUserRole { UserId = 3, RoleId = 2 },

            new SysUserRole { UserId = 4, RoleId = 3 },
            new SysUserRole { UserId = 5, RoleId = 4 },
        };
    }

    /// <summary>
    /// api资源数据
    /// </summary>
    /// <returns></returns>
    public static List<SysApiResource> GetSysApiResources()
    {
        return new List<SysApiResource>()
        {
            new SysApiResource { Id = 1, Name = "分页获取用户列表", Code = "", LinkUrl = "/api/user/getusersbypage", Sort = 1, CreateBy = 1 },
            new SysApiResource { Id = 2, Name = "分页获取角色列表", Code = "", LinkUrl = "/api/role/getrolesbypage", Sort = 2, CreateBy = 1 },
            new SysApiResource { Id = 3, Name = "分页获取api接口列表", Code = "", LinkUrl = "/api/apiresource/getapiresourcesbypage", Sort = 3, CreateBy = 1 },
            new SysApiResource { Id = 4, Name = "获取全部菜单列表", Code = "", LinkUrl = "/api/menu/getmenus", Sort = 4, CreateBy = 1 },
            new SysApiResource { Id = 5, Name = "获取全部角色列表", Code = "", LinkUrl = "/api/role/getroles", Sort = 5, CreateBy = 1 },
            new SysApiResource { Id = 6, Name = "获取全部权限列表", Code = "", LinkUrl = "/api/permission/getpermissions", Sort = 6, CreateBy = 1 },
            new SysApiResource { Id = 7, Name = "获取角色权限列表", Code = "", LinkUrl = "/api/permission/getrolepermissions", Sort = 7, CreateBy = 1 },
            new SysApiResource { Id = 8, Name = "获取全部api接口列表", Code = "", LinkUrl = "/api/apiresource/getapiresources", Sort = 8, CreateBy = 1 },
            new SysApiResource { Id = 9, Name = "获取菜单操作功能", Code = "", LinkUrl = "/api/operation/getoperationsbymenuid", Sort = 9, CreateBy = 1 },

            new SysApiResource { Id = 10, Name = "添加用户", Code = "", LinkUrl = "/api/user/adduser", Sort = 10, CreateBy = 1 },
            new SysApiResource { Id = 11, Name = "编辑用户", Code = "", LinkUrl = "/api/user/edituser", Sort = 11, CreateBy = 1 },
            new SysApiResource { Id = 12, Name = "删除用户", Code = "", LinkUrl = "/api/user/deleteuser", Sort = 12, CreateBy = 1 },
            new SysApiResource { Id = 13, Name = "根据用户获取角色", Code = "", LinkUrl = "/api/role/getrolesbyuserid", Sort = 13, CreateBy = 1 },
            new SysApiResource { Id = 14, Name = "为用户分配角色", Code = "", LinkUrl = "/api/user/distributionrole", Sort = 14, CreateBy = 1 },

            new SysApiResource { Id = 15, Name = "添加角色", Code = "", LinkUrl = "/api/role/addrole", Sort = 15, CreateBy = 1 },
            new SysApiResource { Id = 16, Name = "编辑角色", Code = "", LinkUrl = "/api/role/editrole", Sort = 16, CreateBy = 1 },
            new SysApiResource { Id = 17, Name = "删除角色", Code = "", LinkUrl = "/api/role/deleterole", Sort = 17, CreateBy = 1 },

            new SysApiResource { Id = 18, Name = "添加接口", Code = "", LinkUrl = "/api/apiresource/addapiresource", Sort = 18, CreateBy = 1 },
            new SysApiResource { Id = 19, Name = "编辑接口", Code = "", LinkUrl = "/api/apiresource/editapiresource", Sort = 19, CreateBy = 1 },
            new SysApiResource { Id = 20, Name = "删除接口", Code = "", LinkUrl = "/api/apiresource/deleteapiresource", Sort = 20, CreateBy = 1 },

            new SysApiResource { Id = 21, Name = "添加菜单", Code = "", LinkUrl = "/api/menu/addmenu", Sort = 21, CreateBy = 1 },
            new SysApiResource { Id = 22, Name = "编辑菜单", Code = "", LinkUrl = "/api/menu/editmenu", Sort = 22, CreateBy = 1 },
            new SysApiResource { Id = 23, Name = "删除菜单", Code = "", LinkUrl = "/api/menu/deletemenu", Sort = 23, CreateBy = 1 },

            new SysApiResource { Id = 24, Name = "保存角色权限", Code = "", LinkUrl = "/api/permission/saverolepermission", Sort = 24, CreateBy = 1 },

            new SysApiResource { Id = 25, Name = "添加菜单功能", Code = "", LinkUrl = "/api/operation/addoperation", Sort = 25, CreateBy = 1 },
            new SysApiResource { Id = 26, Name = "编辑菜单功能", Code = "", LinkUrl = "/api/operation/editoperation", Sort = 26, CreateBy = 1 },
            new SysApiResource { Id = 27, Name = "删除菜单功能", Code = "", LinkUrl = "/api/operation/deleteoperation", Sort = 27, CreateBy = 1 },
        };
    }

    /// <summary>
    /// 菜单数据
    /// </summary>
    /// <returns></returns>
    public static List<SysMenu> GetSysMenus()
    {
        return new List<SysMenu>() {
            new SysMenu { Id = 1, Name = "平台管理", ParentId = 0, Route = "/platform", Icon = "platform", Enabled = true, Sort = 1, CreateBy = 1 },
            new SysMenu { Id = 2, Name = "菜单管理", ParentId = 1, Route = "/platform/menu", Icon = "menu", Enabled = true, Sort = 1, CreateBy = 1 },
            new SysMenu { Id = 3, Name = "接口管理", ParentId = 1, Route = "/platform/api", Icon = "api", Enabled = true, Sort = 2, CreateBy = 1 },
            new SysMenu { Id = 4, Name = "功能操作", ParentId = 1, Route = "/platform/operation", Icon = "operation", Enabled = true, Sort = 3, CreateBy = 1 },
            new SysMenu { Id = 5, Name = "租户管理", ParentId = 1, Route = "/platform/tenant", Icon = "tenant", Enabled = true, Sort = 4, CreateBy = 1 },

            new SysMenu { Id = 6, Name = "系统管理", ParentId = 0, Route = "/system", Icon = "system", Enabled = true, Sort = 2, CreateBy = 1 },
            new SysMenu { Id = 7, Name = "用户管理", ParentId = 6, Route = "/system/user", Icon = "user", Enabled = true, Sort = 1, CreateBy = 1 },
            new SysMenu { Id = 8, Name = "角色管理", ParentId = 6, Route = "/system/role", Icon = "role", Enabled = true, Sort = 2, CreateBy = 1 },
            new SysMenu { Id = 9, Name = "权限分配", ParentId = 6, Route = "/system/permission", Icon = "permission", Enabled = true, Sort = 3, CreateBy = 1 },

            new SysMenu { Id = 10, Name = "个人管理", ParentId = 0, Route = "/person", Icon = "personalCenter", Enabled = true, Sort = 3, CreateBy = 1 },
            new SysMenu { Id = 11, Name = "个人中心", ParentId = 10, Route = "/person/center", Icon = "personalCenter", Enabled = true, Sort = 1, CreateBy = 1 },
        };
    }

    /// <summary>
    /// 功能操作数据
    /// </summary>
    /// <returns></returns>
    public static List<SysOperation> GetSysOperations()
    {
        return new List<SysOperation>()
        {
            new SysOperation { Id = 1, Name = "分页获取用户列表", Code = "getusersbypage", ApiResourceId = 1, ParentId = 0 , Sort = 1,MenuId = 7, CreateBy = 1},
            new SysOperation { Id = 2, Name = "新增用户", Code = "adduser", ApiResourceId = 10, ParentId = 0 , Sort = 2,MenuId = 7, CreateBy = 1},
            new SysOperation { Id = 3, Name = "编辑用户", Code = "getusersbypage", ApiResourceId = 11, ParentId = 0 , Sort = 3,MenuId = 7, CreateBy = 1},
            new SysOperation { Id = 4, Name = "删除用户", Code = "getusersbypage", ApiResourceId = 12, ParentId = 0 , Sort = 4,MenuId = 7, CreateBy = 1},
            new SysOperation { Id = 5, Name = "获取全部角色列表", Code = "getroles", ApiResourceId = 5, ParentId = 0 , Sort = 5,MenuId = 7, CreateBy = 1},
            new SysOperation { Id = 6, Name = "根据用户获取角色", Code = "getrolesbyuserid", ApiResourceId = 13, ParentId = 0 , Sort = 6,MenuId = 7, CreateBy = 1},
            new SysOperation { Id = 7, Name = "为用户分配角色", Code = "distributionrole", ApiResourceId = 14, ParentId = 0 , Sort = 7,MenuId = 7, CreateBy = 1},

            new SysOperation { Id = 8, Name = "分页获取角色列表", Code = "getrolesbypage", ApiResourceId = 2, ParentId = 0 , Sort = 8,MenuId = 8, CreateBy = 1},
            new SysOperation { Id = 9, Name = "添加角色", Code = "addrole", ApiResourceId = 15, ParentId = 0 , Sort = 9,MenuId = 8, CreateBy = 1},
            new SysOperation { Id = 10, Name = "编辑角色", Code = "editrole", ApiResourceId = 16, ParentId = 0 , Sort = 10,MenuId = 8, CreateBy = 1},
            new SysOperation { Id = 11, Name = "删除角色", Code = "deleterole", ApiResourceId = 17, ParentId = 0 , Sort = 11,MenuId = 8, CreateBy = 1},

            new SysOperation { Id = 12, Name = "分页获取api接口列表", Code = "getapiresourcesbypage", ApiResourceId = 3, ParentId = 0 , Sort = 12,MenuId = 3, CreateBy = 1},
            new SysOperation { Id = 13, Name = "添加接口", Code = "addapi", ApiResourceId = 18, ParentId = 0 , Sort = 13,MenuId = 3, CreateBy = 1},
            new SysOperation { Id = 14, Name = "编辑接口", Code = "editapi", ApiResourceId = 19, ParentId = 0 , Sort = 14,MenuId = 3, CreateBy = 1},
            new SysOperation { Id = 15, Name = "删除接口", Code = "deleteapi", ApiResourceId = 20, ParentId = 0 , Sort = 15,MenuId = 3, CreateBy = 1},

            new SysOperation { Id = 16, Name = "获取全部菜单列表", Code = "getmenus", ApiResourceId = 4, ParentId = 0 , Sort = 16,MenuId = 2, CreateBy = 1},
            new SysOperation { Id = 17, Name = "添加菜单", Code = "addmenu", ApiResourceId = 21, ParentId = 0 , Sort = 17,MenuId = 2, CreateBy = 1},
            new SysOperation { Id = 18, Name = "编辑菜单", Code = "editmenu", ApiResourceId = 22, ParentId = 0 , Sort = 18,MenuId = 2, CreateBy = 1},
            new SysOperation { Id = 19, Name = "删除菜单", Code = "deletemenu", ApiResourceId = 23, ParentId = 0 , Sort = 19,MenuId = 2, CreateBy = 1},

            new SysOperation { Id = 20, Name = "获取全部菜单列表", Code = "getmenus", ApiResourceId = 4, ParentId = 0 , Sort = 20,MenuId = 4, CreateBy = 1},
            new SysOperation { Id = 21, Name = "获取菜单操作功能", Code = "getoperationsbymenuid", ApiResourceId = 9, ParentId = 0 , Sort = 21,MenuId = 4, CreateBy = 1},
            new SysOperation { Id = 22, Name = "添加菜单功能", Code = "addoperation", ApiResourceId = 25, ParentId = 0 , Sort = 22,MenuId = 4, CreateBy = 1},
            new SysOperation { Id = 23, Name = "编辑菜单功能", Code = "editoperation", ApiResourceId = 26, ParentId = 0 , Sort = 23,MenuId = 4, CreateBy = 1},
            new SysOperation { Id = 24, Name = "删除菜单功能", Code = "deleteoperation", ApiResourceId = 27, ParentId = 0 , Sort = 24,MenuId = 4, CreateBy = 1},
            new SysOperation { Id = 25, Name = "获取全部api接口列表", Code = "getapiresources", ApiResourceId = 8, ParentId = 0 , Sort = 25,MenuId = 4, CreateBy = 1},

            new SysOperation { Id = 26, Name = "获取全部角色列表", Code = "getroles", ApiResourceId = 5, ParentId = 0 , Sort = 26,MenuId = 9, CreateBy = 1},
            new SysOperation { Id = 27, Name = "获取全部权限列表", Code = "getpermissions", ApiResourceId = 6, ParentId = 0 , Sort = 27,MenuId = 9, CreateBy = 1},
            new SysOperation { Id = 28, Name = "获取角色权限列表", Code = "getrolepermissions", ApiResourceId = 7, ParentId = 0 , Sort = 28,MenuId = 9, CreateBy = 1},
            new SysOperation { Id = 29, Name = "保存角色权限", Code = "saverolepermission", ApiResourceId = 24, ParentId = 0 , Sort = 29,MenuId = 9, CreateBy = 1},
        };
    }

    /// <summary>
    /// 获取权限数据
    /// </summary>
    /// <returns></returns>
    public static List<SysPermission> GetSysPermissions()
    {
        var menus = GetSysMenus();
        var permissionMenus = new List<SysPermission>();
        foreach (var menu in menus)
        {
            permissionMenus.Add(new SysPermission { Id = menu.Id, Name = menu.Name, Type = SysConst.MENU, CreateBy = 1 });
        }

        var maxId = permissionMenus.Max(m => m.Id);

        var operations = GetSysOperations();
        var permissionOperations = new List<SysPermission>();
        foreach (var operation in operations)
        {
            permissionOperations.Add(new SysPermission { Id = operation.Id + maxId, Name = operation.Name, Type = SysConst.OPERATION, CreateBy = 1 });
        }

        permissionMenus.AddRange(permissionOperations);
        return permissionMenus;
    }

    /// <summary>
    /// 获取角色权限数据
    /// </summary>
    /// <returns></returns>
    public static List<SysRolePermission> GetSysRolePermissions()
    {
        var permissions = GetSysPermissions().Where(p => p.Id != 5);
        var rolePermissions = new List<SysRolePermission>();
        foreach (var permission in permissions)
        {
            rolePermissions.Add(new SysRolePermission { RoleId = 1, PermissionId = permission.Id });
        }
        return rolePermissions;
    }

    /// <summary>
    /// 获取权限菜单数据
    /// </summary>
    /// <returns></returns>
    public static List<SysPermissionMenu> GetSysPermissionMenus()
    {
        var permissions = GetSysPermissions().Where(p => p.Type == SysConst.MENU).ToList();
        var permissionMenus = new List<SysPermissionMenu>();
        foreach (var permission in permissions)
        {
            permissionMenus.Add(new SysPermissionMenu { PermissionId = permission.Id, MenuId = permission.Id });
        }
        return permissionMenus;
    }

    /// <summary>
    /// 获取权限功能操作数据
    /// </summary>
    /// <returns></returns>
    public static List<SysPermissionOperation> GetSysPermissionOperations()
    {
        var maxId = GetSysPermissions().Where(p => p.Type == SysConst.MENU).Max(p => p.Id);
        var permissions = GetSysPermissions().Where(p => p.Type == SysConst.OPERATION).ToList();
        var permissionMenus = new List<SysPermissionOperation>();
        foreach (var permission in permissions)
        {
            permissionMenus.Add(new SysPermissionOperation { PermissionId = permission.Id, OperationId = permission.Id - maxId });
        }
        return permissionMenus;
    }
}
